Lua Tutorials
Password Door Lock script only! with alarm lock and pass to turn off textutils.slowPrint("Enter Password") correctpass = "Correct Password" alarmshutdown = "Shutdown alarm password" pass = read("*") if pass correctpass then rs.setOutput("right", true) print("Password Accepted") sleep(5) rs.setOutput("right", false) os.reboot() else print("password Incorrect!") rs.setOutput("top", true) -- only if you have an alarm at the top! sleep(5) -- only if you have alarm at the top! print("Pass To Turn Alarm Off") -- only if you have alarm at the top! alarm = read("*") -- only if you have alarm at the top! if alarm alarmshutdown then -- only if you have alarm at the top! rs.setOutput("top", false) -- only if you have alarm at the top! os.reboot() -- only if you have alarm at the top! end end You can copypaste this code into the terminal, then correct the variables ( correctpass, alarmshutdown) to fit your liking. ______________________________ Please add new tutorials in form of new pages to the Category Lua Tutorials instead! ______________________________ =Password Door= Creation If you place a computer on the right hand side of a door (Iron, Wooden or Reinforced), then input the following text. edit Startup This opens up the page to edit what happens on the start up of the computer. Now type: print("Enter Password") In this code, you can change the text in the quotes to any text you want when the computer starts up Now press "Ctrl" then "Enter" to save it, then "Ctrl", "Right Arrow" then "Enter" to close the screen. Then type: edit bananas The word bananas can be substituted for any password but beware, it is case sensitive (e.g. "bananas" would not work with "Bananas"). Now type the following text into this menu. print("Password Accepted") print("Password Accepted") redstone.setOutput("left", true) sleep(3) redstone.setOutput("left", false) os.reboot() The previous script executes the following task After you have input this, press "Ctrl" then "Enter" to save it, then "Ctrl", "Right Arrow" then "Enter" to close the screen. Thief Proofing On a multiplayer server, you may want to do this if you want no one accessing your door. To thief proof a computer you need to edit the file edit list Then input a print command of your choice. E.g. print("No Hacking!!!") After you have input this, press "Ctrl" then "Enter" to save it, then "Ctrl", "Right Arrow" then "Enter" to close the screen. You also need to do the same for the program "Programs". E.g. edit programs Then input a print command of your choice. E.g. print("No Hacking!!!") After you have input this, press "Ctrl" then "Enter" to save it, then "Ctrl", "Right Arrow" then "Enter" to close the screen. However, in any case, this doesn't actually edit the computer's actual commands. The list command still exists, the programs command still exists, etc. The reason that: edit list prevents the list command from being used is because a program called list (but not list itself) is created and saved in the current directory. A user familiar with the computer's file system could simply type the path to the real list command in the 'programs' directory: rom/programs/list When this command is run while in the root directory, the list program located in the computer's ROM is executed instead of the program created by the user. The same could be done if someone has 'locked' the edit command: rom/programs/edit This will manually find the real edit command, and allow to be edited. So remember that this is just a deterrent to brush off the inexperienced hackers. Additions This is the basic password door code. This section will teach you how to add on to that for any better reason. Aesthetics print("Password Accepted") sleep(1) print("Door Open. Please Proceed!") redstone.setOutput("left", true) sleep(3) redstone.setOutput("left", false) os.reboot() Adding the extra sleep(1) print("Door Open. Please Proceed!") Makes the operation look a lot better. Manual Override Adding a second password to keep the door open for as long as you like can help with boring tasks like building a second house and gathering the resources. To add the override, you have to input the text edit USA With "USA" being the example that you can change. Then input the text print("Override Accepted. Reboot To Reset!") redstone.setOutput("left", true) To reset the computer, simply press "Ctrl + R" Hacking This program is not very safe and easy to hack. First type this into the console: lua This will allow you to execute all codes, you can usually use in programs. Now remember the code to turn on redstone and type it in: redstone.setOutput("left", true) And there the door is opened without knowing any password. Preventing this hack This hack is easy to prevent with the method we used before, just type this in: edit lua Now you can add the print text of your choice like already described above: print("Smart, but you failed!") And to prevent users typing "cd rom" which has all the edit, lua etc. already there, type this: edit cd And you can do the same with "edit lua" and make it: print("Nice try, hacker!") All in all this way of making a door lock isn't very safe, because everyone could just start all of the not-denied programs, edit startup or use one of the other methods to bypass this lock. Some people have keycards and will try to "Bump key" the door open. However, using this code we can stop them. First, edit your startup file named "startup". This is the file that is loaded when the computer starts up every time with a restart or when it is first placed. Now type this code: if fs.exists("disk/startup") then fs.delete("disk/startup") print("XD Check your card at your house") This code will check to see if someone has a card in the disk drive(If you didn't add one, remember that they can always add one) and if it does, it checks to see if there is a startup file. If there is, it deletes that file because it is probably a hacker. But you can however, after finishing everything FIRST type in this: edit edit Now, this is very sneaky. You prevent them from editing any files, and you might want to tell them they failed. print("Access Denied. You fail.") Now if you type edit it will print the above text to the screen. Smart huh? The only problem with this password door is a hacker could just place a lever next to the door, or any source of redstone(torch, button) and it will open the door. Simple Arithmetic Calculator Now that you have made a doorlock, time to make the next step. A calculator! First type into the computer: edit calc Now, type in this code, line by line. Exactly. os.pullEvent = os.pullEventRaw term.clear() term.setCursorPos(1,1) write("Welcome! Please choose a math function (*,/,+,-) ") x = io.read() if x "*" then write("First number?") fnum = io.read() write("Second Number?") snum = io.read() print("Calculating...") term.clear() term.setCursorPos(1,1) answer = fnum * snum print("Your answer is "..answer) sleep(5) elseif x "+" then write("First number?") fnum = io.read() write("Second Number?") snum = io.read() print("Calculating...") term.clear() term.setCursorPos(1,1) answer = fnum + snum print("Your answer is "..answer) sleep(5) elseif x "-" then write("First Number? ") fnum = io.read() write("Second number? ") snum = io.read() answer = fnum - snum print("Your answer is"..answer) sleep(5) elseif x "/" then write("First Number? ") fnum = io.read() write("Second Number? ") snum = io.read() answer = fnum / snum print("Your answer is"..answer) sleep(5) else print("That isn't a valid operation!") sleep(2) os.reboot() end That's it! Just input that into a computer, save it, and run it, you will have a fully working Calculator! -Imran, the Lua Engineer! Digital Clock I know people would like to know what time it is, without going into terminal, so I developed a program to display the time on a monitor. Since the only way to stop the program is to break the computer or the monitor attached (IDK why ctrl+t doesn't work) you should use a disk drive to save the program. Make sure the drive is next to the computer, and has a disk in it. Once that is done type: edit disk/clock Now type this in as the program's code (type it in as is for now, you will edit a few things later): local mon = peripheral.wrap("top") function round(num, idp) local mult = 10^ (idp or 0) return math.floor(num * mult +0.5) / mult end function loop() while true do mon.clear() mon.setCursorPos(1,1) mon.setTextScale(3) z = os.time() if z < 12 then y = z a = y%1 h = y - a b = a * 60 m = round(b) if m >= 10 then min = m elseif m < 10 then min = "0"..m end if h < 1 then hour = 12 elseif h >= 1 then hour = h end x = hour..":"..min.." AM" elseif z > 12 then y = z - 12 a = y%1 h = y - a b = a * 60 m = round(b) if m >= 10 then min = m elseif m < 10 then min = "0"..m end if h < 1 then hour = 12 elseif h >= 1 then hour = h end x = hour..":"..min.." PM" end mon.write(x) sleep(1) end end loop() The things you can edit to your liking are (depends on screen size aka amount of monitors you have): mon.setCursorPos(#,#) --changes where the time will be printed on the screen mon.setTextScale(#) --changes size font of the time on the screen What you need to change depending on which side your monitors are on: local mon = peripheral.wrap("SIDE") --"SIDE" can be changed to "top", "bottom", "left", "right", "front", or "back"—as far as this code goes, it only outputs to a single side Now when you have that all done, you can go ahead and run the program: disk/clock NOTE: While you know shutting off the program is annoying, you also cannot use the computer while the program is running. Now you have a continuously running clock, without any LED's or redstone wiring! If your wondering, the way I set mine up uses 3 monitors, horizontally on top of the computer, hence the: local mon = peripheral.wrap("top") --monitors on top of computer mon.setCursorPos(1,1) --time display starts wher left-most monitor starts mon.setTextScale(3) --makes the time fit nicely across the 3 monitors -Jim, Multi-Language Programmer Simpler Clock Category:ComputerCraft Category:Lua Tutorials Category:Tutorials